package com.jie.demo1.pojo;

import com.jie.demo1.pojo.menu.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data

/**
 * 全局统一返回结果类
 */

public class Result<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    //无参构造器
    public Result() {}

    //将传入的泛型指定为该result的泛型
    private static <T> Result<T> build(T data) {
        Result<T> result = new Result<>();
        if (data != null) result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }


    public static <T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok() {
        return Result.ok(null);
    }

//构建一个失败的方法，返回一个失败的结果result，第二个参数是一个枚举类型的对象，用于设置返回结果的状态码和消息
    public static <T> Result<T> fail() {
        return build(null, ResultCodeEnum.FAIL);
    }

    public static <T> Result<T> fail(Integer code, String message) {
        Result<T> result = build(null);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }


}
